#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "comm.h"
#include "menu.h"
#include "dlist.h"
#include "device.h"

int g_modify_flag = 0; // 数据是否发生变化的标志

int main()
{
	node_t *head = NULL;
	int ret = 0;

	// 加载设备文件device.dat配置信息到链表
	head = load_dev();
	if(!head)
	{
		printf("load_dev failed\n");
		return -1;
	}
	
	// 进入欢迎界面
	ret = welcome_menu(head);
	if('q' == ret)
	{
		printf("save file(y/n)? ");
		int save_op = getchar();
		empty_buffer();
		if('y' == save_op)
		{
			// 保存设备链表信息到文件
			ret = save_dev(head);
			if(ret < 0)
			{
				printf("save_dev failed\n");
			}
		}
		else if('n' == save_op)
		{
			return 0;
		}
		else if(g_modify_flag == 1)
		{
			ret = save_dev(head);
			if(ret < 0)
			{
				printf("save_dev failed\n");
			}
		}
		else
		{
			printf("Wrong input.\n");
			return -1;
		}
	}

	destroy_dlist(&head);

	g_list_head = NULL;

	return ret;
}
